package org.springframework.boot.netty.argumentResolver;


import org.springframework.boot.netty.listener.Message;

import java.util.List;

public class ReturnValueArgumentResolverComposite extends ReturnValueArgumentResolverAdapter {


   private final List<ReturnValueArgumentResolver> returnValueArgumentResolverComposite ;


   public ReturnValueArgumentResolverComposite(List<ReturnValueArgumentResolver> returnValueArgumentResolverComposite){
        this.returnValueArgumentResolverComposite = returnValueArgumentResolverComposite;
    }

    @Override
    public boolean supportsParameter(MethodParameter methodParameter){

        for (ReturnValueArgumentResolver returnValueArgumentResolver : returnValueArgumentResolverComposite) {
          if(returnValueArgumentResolver.supportsParameter(methodParameter)){
              return true;
          }
        }
        return false;
    }

    @Override
    public void resolveArgument(Message message, Object returnValue, MethodParameter methodParameter) throws Exception{

        ReturnValueArgumentResolver returnValueArgumentResolver = findArgumentResolver(methodParameter);
        returnValueArgumentResolver.resolveArgument(message,returnValue,methodParameter);
    }

    private ReturnValueArgumentResolver findArgumentResolver(MethodParameter methodParameter){
        for (ReturnValueArgumentResolver returnValueArgumentResolver : returnValueArgumentResolverComposite) {
            if(returnValueArgumentResolver.supportsParameter(methodParameter)){
                return returnValueArgumentResolver;
            }
        }
        return null;
    }


}